CREATE PROCEDURE [dbo].[asi_GetShipPrice]
@originKey uniqueidentifier,
@shipMethodName nvarchar(100),
@postalCode nvarchar(20),
@postalCodeFragmentLength int,
@countryCode nvarchar(50),
@shipType int,
@shipPriceValue decimal(18,4)
AS
declare @postalCodeFragment nvarchar(20)
set @postalCodeFragment = substring(@postalCode, 1, @postalCodeFragmentLength)
SELECT
COALESCE
(
(SELECT
TOP 1 sp.Price
FROM
ShipZone sz INNER JOIN ShipPrice sp
ON sz.ShipZoneKey = sp.ShipZoneKey
INNER JOIN ShipMethod sm
ON sp.ShipMethodKey = sm.ShipMethodKey
WHERE
sz.ShipOriginKey = @originKey
AND sm.Name = @shipMethodName
AND (sz.TargetRangeBegin = @postalCode
AND sz.TargetRangeEnd = @postalCode)
AND sp.PricingTypeCode = @shipType
AND sp.MinAmount <= @shipPriceValue
AND sz.CountryCode = @countryCode
ORDER BY
sp.MinAmount DESC
),
(SELECT
TOP 1 sp.Price
FROM
ShipZone sz INNER JOIN ShipPrice sp
ON sz.ShipZoneKey = sp.ShipZoneKey
INNER JOIN ShipMethod sm
ON sp.ShipMethodKey = sm.ShipMethodKey
WHERE
sz.ShipOriginKey = @originKey
AND sm.Name = @shipMethodName
AND (sz.TargetRangeBegin <= @postalCodeFragment
AND sz.TargetRangeEnd >= @postalCodeFragment)
AND sp.PricingTypeCode = @shipType
AND sp.MinAmount <= @shipPriceValue
AND sz.CountryCode = @countryCode
ORDER BY
sp.MinAmount DESC
),
(SELECT
TOP 1 sp.Price
FROM
ShipZone sz INNER JOIN ShipPrice sp
ON sz.ShipZoneKey = sp.ShipZoneKey
INNER JOIN ShipMethod sm
ON sp.ShipMethodKey = sm.ShipMethodKey
WHERE
sz.ShipOriginKey = @originKey
AND sm.Name = @shipMethodName
AND (sz.TargetRangeBegin = ''
AND sz.TargetRangeEnd = '')
AND sp.PricingTypeCode = @shipType
AND sp.MinAmount <= @shipPriceValue
AND sz.CountryCode = @countryCode
ORDER BY
sp.MinAmount DESC
),
-1)
GO